//
//  XLHomeViewController.swift
//  WeiBo
//
//  Created by macbook on 16/10/17.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit


//cell的重用标识
private let orgintCellID = "orginaCell"

class XLHomeViewController: XLVistorTableViewController {

    //定义一个可变数组来放模型,并制定类型
    var statusArrM:[XLStatus]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !isLogin {
            
            vistorView?.setupVistorInfor(nil, title: nil)
        }
        
        //注册cell
        setupTableViewCellRegisClass()
        
        //数据
        loadStatusDate { (result) in
            
            self.statusArrM = result;
            self.tableView.reloadData()
            
           }
     }
    
    func setupTableViewCellRegisClass()
    {
        //自动计算行高
       tableView.rowHeight = UITableViewAutomaticDimension
       tableView.estimatedRowHeight = 200
        
        tableView.registerClass(XLOrginaTableViewCell.self, forCellReuseIdentifier: orgintCellID)
    }


}


extension XLHomeViewController
{
    func loadStatusDate(callBack:(result:[XLStatus])->())
    {
            XLNetworkTools.shardTools.homeLoadStatusData({ (reponse) in
                
                //判断是否有值,是否是数组
                guard let repo = reponse?["statuses"] as? [[String:AnyObject]]else
                {
                    return
                }
                
                //实例一个可变数组
                var temeArray:[XLStatus] = [XLStatus]()
                
                for dict in repo
                {
                    //字典转模型
                    let statusDome = XLStatus(dict: dict)
                    
                    temeArray.append(statusDome)
                 }
                //执行完毕返回去
                callBack(result: temeArray)
                
           
            }) { (error) in
                print("请求首页数据错误\(error)")
        }
    }
}

//数据源方法
extension XLHomeViewController
{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusArrM?.count ?? 0
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(orgintCellID) as! XLOrginaTableViewCell
        
        cell.backgroundColor = RandomColor()
        cell.status = statusArrM![indexPath.row]
        return cell
        
        
    }
    
}










